package com.caszx.chargingpile.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONException;
import com.app.base.app.BaseTopBarDelayActivity;
import com.app.utils.DipPxUtil;
import com.app.utils.DiskUser;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.utils.DialogHelp;
import com.jakewharton.rxbinding.view.RxView;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;
import rx.functions.Action1;

public class UserAccountActivity extends BaseTopBarDelayActivity  {



    private TextView  txt_phone;
    private TextView  txt_balance;
    private TextView  txt_consume;
    private TextView  txt_md_passwd;
    DecimalFormat decimalFormat = new DecimalFormat("##.##");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_account);
        setCenterTitle("账户");
    }

    @Override
    protected void findView() {
        txt_phone = (TextView) findViewById(R.id.txt_phone);
        txt_balance = (TextView) findViewById(R.id.txt_balance);
        txt_consume = (TextView) findViewById(R.id.txt_consume);
        txt_md_passwd = (TextView) findViewById(R.id.txt_md_passwd);
        int bound = DipPxUtil.dip2px(this, 20);
        Drawable right_arrow = getResources().getDrawable(R.drawable.selector_right_arrow);
        right_arrow.setBounds(0, 0, bound, bound);
        txt_md_passwd.setCompoundDrawables(null, null, right_arrow, null);
        RxView.clicks(txt_md_passwd).throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Intent intent = new Intent(UserAccountActivity.this,UserMdPassWdActivity.class);
                        startActivity(intent);
                    }
                });

        getUserInfo();
    }

    private void upDataUser(JSONObject jsonObject){
        if(jsonObject !=null){
            txt_phone.setText("账户\u0020\u0020\u0020"+jsonObject.optString("account"));
            txt_balance.setText("余额\u0020\u0020\u0020"+decimalFormat.format(jsonObject.optDouble("balance")));
//            txt_consume.setText("消费\u0020\u0020\u0020"+jsonObject.optInt("countCost"));
        }
    }

    private void getUserInfo(){
      showProgressDialog("");
        HashMap<String,String> params = new HashMap<>();
        String token = DiskUser.getStringValueByKey(this,"token");
        params.put("token",token);
        OkHttpUtils.post()
                .url(ConstantUrl.URL_USER_DETAIL)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        dissmissProgressDialog();
                         DialogHelp.showDialogOnlyMesg(UserAccountActivity.this,getResources().getString(R.string.error_network),null);
                        //showSnackbarMsg(getResources().getString(R.string.error_network));
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        dissmissProgressDialog();
                        JSONObject resultJsonObject =  response.optJSONObject("result");
                        if(resultJsonObject!=null){
                            int code =  resultJsonObject.optInt("code");
                            if(code<0){
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"),null);
                                upDataUser(null);
                            }else{
                                try {
                                    upDataUser(response);
                                }catch (JSONException e){

                                }
                            }
                        }
                    }
                });
    }
}
